// ***************************综合情况*****************************
app.controller('homeCtrl', function ($scope, $http, $location, $rootScope, $filter) {

    var routerArr = ['speedStateCtrl', 'workList', "investigation", "registerBook", "aggrege", "stage"];
    $scope.goToRoute = function (activeIdx, routeIdx, par) {
        $rootScope.activeId = activeIdx - 0;

        var startRouter = "/home/";
        var routerStr = startRouter + routerArr[routeIdx - 1];
        if (par != null) {
            if (par == 1) {
                $rootScope.navType = 'nc'
            }
            if (par == 2) {
                $rootScope.navType = 'jt'
            }
            if (par == 3) {
                $rootScope.workListNavType = 'ssfa'
            }
            if (par == 4) {
                $rootScope.workListNavType = 'dcjggs'
            }
            if (par == 5) {
                $rootScope.workListNavType = 'sjjk'
            }
            if (par == 6) {
                $rootScope.workListNavType = 'nyjc'
            }
            if (par == 7) {
                $rootScope.workListNavType = 'wyjc'
            }
            if (par == 8) {
                $rootScope.workListNavType = 'sxlr'
            }
            if (par == 9) {
                $rootScope.workListNavType = 'yhyddy'
            }
        }
        $location.path(routerStr);

    };

    $http.post("http://120.24.219.92:8899/zhqk")
        .then(function (result) {
            var msg = result.data.msg;
            if (msg==null){
                $location.path("/login");
            }
            $scope.stateArr = result.data.data;
            // 权籍调查
            // 农村宅基地
            $scope.investigation = $scope.stateArr[0];
            var investigationValue = $scope.stateArr[0].wcs / $scope.stateArr[0].zs;
            var investigationData = [investigationValue, 1 - investigationValue];
            if (investigationValue < 0.69) {
                var investigationColor = ['#ff2b42', '#334E61']
            } else if (investigationValue < 0.9) {
                var investigationColor = ['#ff8f00', '#334E61']
            } else {
                var investigationColor = ['#00cb00', '#334E61']
            }
            var investigationOption1 = {
                color: investigationColor,
                series: [
                    {
                        hoverAnimation: false,
                        name: '权籍调查-农村宅基地',
                        type: 'pie',
                        radius: ['80%', '90%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                textStyle: {
                                    color: investigationColor[0],
                                    fontSize: '18',
                                },
                                formatter: (investigationValue * 100).toFixed(2) + '%'
                            },
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: investigationData
                    }
                ]
            };
            var investigationChart = echarts.init(document.getElementById("investigationOption1"));
            investigationChart.setOption(investigationOption1);

            // 集体建设用地
            $scope.investigation2 = $scope.stateArr[2];
            var investigation2Value = $scope.stateArr[2].wcs / $scope.stateArr[2].zs;
            var investigation2Data = [investigation2Value, 1 - investigation2Value];
            if (investigation2Value < 0.69) {
                var investigation2Color = ['#ff2b42', '#334E61']
            } else if (investigation2Value < 0.9) {
                var investigation2Color = ['#ff8f00', '#334E61']
            } else {
                var investigation2Color = ['#00cb00', '#334E61']
            }
            var investigation2Option2 = {
                color: investigation2Color,
                series: [
                    {
                        hoverAnimation: false,
                        name: '权籍调查-农村宅基地',
                        type: 'pie',
                        radius: ['80%', '90%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                textStyle: {
                                    color: investigation2Color[0],
                                    fontSize: '18',
                                },
                                formatter: (investigation2Value * 100).toFixed(2) + '%'
                            },
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: investigation2Data
                    }
                ]
            };
            var investigation2Chart = echarts.init(document.getElementById("investigationOption2"));
            investigation2Chart.setOption(investigation2Option2);

            // 编辑成图
            // 农村宅基地
            $scope.register = $scope.stateArr[1];
            var registerValue = $scope.stateArr[1].wcs / $scope.stateArr[1].zs;
            var registerData = [registerValue, 1 - registerValue];
            if (registerValue < 0.69) {
                var registerColor = ['#ff2b42', '#334E61']
            } else if (registerValue < 0.9) {
                var registerColor = ['#ff8f00', '#334E61']
            } else {
                var registerColor = ['#00cb00', '#334E61']
            }
            var registerOption1 = {
                color: registerColor,
                series: [
                    {
                        hoverAnimation: false,
                        name: '权籍调查-农村宅基地',
                        type: 'pie',
                        radius: ['80%', '90%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                textStyle: {
                                    color: registerColor[0],
                                    fontSize: '18',
                                },
                                formatter: (registerValue * 100).toFixed(2) + '%'
                            },
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: registerData
                    }
                ]
            };
            var registerChart = echarts.init(document.getElementById("registerOption1"));
            registerChart.setOption(registerOption1);

            // 集体建设用地
            $scope.register2 = $scope.stateArr[3];
            var register2Value = $scope.stateArr[3].wcs / $scope.stateArr[3].zs;
            var register2Data = [register2Value, 1 - register2Value];
            if (register2Value < 0.69) {
                var register2Color = ['#ff2b42', '#334E61']
            } else if (register2Value < 0.9) {
                var register2Color = ['#ff8f00', '#334E61']
            } else {
                var register2Color = ['#00cb00', '#334E61']
            }
            var register2Option2 = {
                color: register2Color,
                series: [
                    {
                        hoverAnimation: false,
                        name: '权籍调查-农村宅基地',
                        type: 'pie',
                        radius: ['80%', '90%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                textStyle: {
                                    color: register2Color[0],
                                    fontSize: '18',
                                },
                                formatter: (register2Value * 100).toFixed(2) + '%'
                            },
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: register2Data
                    }
                ]
            };
            var register2Chart = echarts.init(document.getElementById("registerOption2"));
            register2Chart.setOption(register2Option2);

            // 工作组织
            // $scope.stateData = {
            //     mobilize: $scope.stateArr[4].wcs / $scope.stateArr[4].zs,
            //     organize: $scope.stateArr[5].wcs / $scope.stateArr[5].zs,
            //     bid: $scope.stateArr[6].wcs / $scope.stateArr[6].zs,
            //     notice: $scope.stateArr[7].wcs / $scope.stateArr[7].zs
            // };
        });

    var data = [], oDate = new Date();
    var i=oDate.getDay();
    if(i==0){
        $scope.oldDate = $filter("date")(oDate - 86400000*6, "yyyy-MM-dd");
    }else {
        $scope.oldDate = $filter("date")(oDate - 86400000*(i-1), "yyyy-MM-dd");
    }
    $scope.currentDate = $filter("date")(oDate, "yyyy-MM-dd");
    // for(var m = 0; m < 14 ; m++){
    //     var date ={};
    //     date.id = 14 - m;
    //     var dayArr = new Date(oDate.getFullYear(),oDate.getMonth(),oDate.getDate() - m);
    //     date.endDate = $filter("date")(dayArr, "yyyy-MM-dd");
    //     data.push(date);
    // }
    // $scope.startTime = data[0].endDate;
    // var startDateArr = [];
    // var endDateArr = [];
    // var n = 1;
    // if (data.length < 2) {
    //     n = 0
    // }
    // for (var i = 0; i < data.length; i++) {
    //     startDateArr.push({id: data[i].id, value: data[i].endDate});
    //     endDateArr.push({id: data[i].id, value: data[i].endDate});
    // }
    // if (n != 0) {
    //     startDateArr.splice(0, 1);
    //     endDateArr.splice(endDateArr.length - 1, 1);
    // }
    //默认值
    // getIncressData(data[n].endDate, data[0].endDate);
    getIncressData($scope.oldDate, $scope.currentDate);
    // getIncressData("2020-04-05","2020-04-06");
    // $('#trigger2').html(`
    //             <i class="icon-down background-size" ></i>
    //             <span class="all-state-title-time">
    //             ` + data[0].endDate + `
    //             </span>
    //             <span class="all-state-title-time" > 至 </span>
    //             <span class="all-state-title-time">
    //             ` + data[n].endDate + `
    //             </span>`);
    //
    // var mobileSelect2 = new MobileSelect({
    //     trigger: '#trigger2',
    //     title: '选择时间',
    //     titleColor: '#0c2344',
    //     ensureBtnColor: '#e5fffd',
    //     cancelBtnColor: '#e5fffd',
    //     textColor: 'rgba(12,35,68,0.8)',
    //     wheels: [
    //         {data: startDateArr},
    //         {data: endDateArr}
    //     ],
    //     triggerDisplayData: false,
    //     onShow: function (e) {
    //         // $($('.mobileSelect-show .selectContainer')[0]).children().eq(0).css('color','#FF931D');
    //     },
    //     transitionEnd: function (indexArr, data) {
    //         var startDate = new Date(data[0].value);
    //         var endDate = new Date(data[1].value);
    //         if (endDate <= startDate) {
    //             $('.ensure').addClass("disable");
    //             $.message({
    //                 message: '查询的结束时间不能早于开始时间',
    //                 type: 'error'
    //             });
    //         } else {
    //             $('.ensure').removeClass("disable")
    //         }
    //     },
    //     callback: function (indexArr, data) {
    //
    //         $('#trigger2').html(`
    //                 <i class="icon-down background-size" ></i>
    //         <span class="all-state-title-time">
    //         ` + data[n].value + `
    //         </span>
    //         <span class="all-state-title-time" > 至 </span>
    //         <span class="all-state-title-time">
    //         ` + data[0].value + `
    //         </span>`);
    //         getIncressData(data[0].value, data[1].value);
    //     }
    // });


    $scope.coverShow = false;
    $scope.selectTime = function () {
        $scope.coverShow = true;
    };

    $scope.incressData = {
        农村宅基地权籍调查: {
            name: "",
            wcs: "",
            zs: ""
        },
        农村宅基地不动产测量: {
            name: "",
            wcs: "",
            zs: ""
        },
        集体建设用地权籍调查: {
            name: "",
            wcs: "",
            zs: ""
        },
        集体建设用地不动产测量: {
            name: "",
            wcs: "",
            zs: ""
        }
    };
    function getIncressData(start, end) {
        $http.post("http://120.24.219.92:8899/qybh", {startDate: start, endDate: end}).then(function (result) {
            for (let i = 0; i < result.data.data.length; i++) {
                $scope.incressData[result.data.data[i].name] = result.data.data[i];
            }
        })
    }
});

// ***************************进度指标*****************************
app.controller('speedStateCtrl', function ($scope, $http, $rootScope, $filter) {
    // 权籍调查情况
    // 部门升降序、
    $scope.titleName = '权籍调查';
    $scope.investigationNavActive = '0';
    getIncressData('0');
    $scope.investigationNavClick = function (name) {
        $scope.investigationNavActive = name;
        if (name == '1') {
            $scope.titleName = '编辑成图';
            getIncressData(name);
        }
        if (name == '0') {
            $scope.titleName = '权籍调查';
            getIncressData(name);
        }
    };

    function getIncressData(name) {
        $http.post("http://120.24.219.92:8899/tongjidept")
            .then(function (result) {
                var data = result.data.data;
                $scope.investigationCurrentCount = 0;
                $scope.investigationTotal = 0;
                if (name == '0') {
                    for (let i = 0; i < data.length; i++) {
                        $scope.investigationCurrentCount += data[i].qsdcwcs;
                        $scope.investigationTotal += data[i].zs;
                        data[i].rate = data[i].qsdcwcs / data[i].zs;
                        data[i].wcs = data[i].qsdcwcs;
                    }
                } else if (name == '1') {
                    for (let i = 0; i < data.length; i++) {
                        $scope.investigationCurrentCount += data[i].bdcclwcs;
                        $scope.investigationTotal += data[i].zs;
                        data[i].rate = data[i].bdcclwcs / data[i].zs;
                        data[i].wcs = data[i].bdcclwcs;
                    }
                }
                $scope.investigationRate = $scope.investigationCurrentCount / $scope.investigationTotal;
               /* function sortrate(a, b) {
                    return b.rate - a.rate
                }
                //利用js中的sort方法
                data.sort(sortrate);*/
                $scope.investigationList = data;
                var trainValue = $scope.investigationRate;
                var trainData = [trainValue, trainValue, trainValue];
                if (trainValue < 0.60) {
                    var trainColor = ['#ff2b42']
                } else if (trainValue < 0.9) {
                    var trainColor = ['#ff8f00']
                } else if (trainValue) {
                    var trainColor = ['#00cb00']
                }
                var echartsTrainOption = {

                    series: [{
                        color: trainColor,
                        type: 'liquidFill',
                        radius: '100%',
                        data: trainData,
                        backgroundStyle: {
                            borderWidth: 0,
                            borderColor: 'rgb(255,0,255,0.9)',
                            color: '#334E61'
                        },
                        outline: {
                            show: true,
                            borderDistance: 5,
                            itemStyle: {
                                borderColor: 'rgba(67,209,100,1)',
                                borderWidth: 0
                            }
                        },
                        label: {
                            normal: {
                                formatter: (trainValue * 100).toFixed(2) + '%',
                                textStyle: {
                                    color: '#fff',
                                    fontSize: 18
                                }
                            }
                        }
                    }]
                };
                var echartsTrainChart = echarts.init(document.getElementById("echartsTrain"));
                echartsTrainChart.setOption(echartsTrainOption);
            });
    }

    $scope.currentDate = $filter("date")(new Date(), "yyyy-MM-dd");

    // 部门公示数据列表
    $scope.listDetailShow = false;
    $scope.windowText = '';
    $scope.listClick = function (item) {
        $scope.windowText = item.dept;
        $scope.listDetailShow = true;
        $http.post('http://120.24.219.92:8899/tongjideptbynameandtype', {
            name: item.dept
        }).then(function (result) {
            var data  = result.data.data;
            if($scope.titleName == '权籍调查'){
                for(var i=0;i<data.length;i++){
                    data[i].wcs = data[i].qsdcwcs;
                }
            }else if($scope.titleName == '编辑成图'){
                for(var i=0;i<data.length;i++){
                    data[i].wcs = data[i].bdcclwcs;
                }
            }
            console.log(data);
            $scope.DetailList = data;
        })
    };
    $scope.closeWindow = function () {
        $scope.listDetailShow = false;
    };
    $scope.windowTime = '';

    $scope.excel = function () {
        if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
            $('.ensure').addClass("disable");
            $.message({
                message: '移动端不支持导出功能，请在PC端操作！',
                type: 'error'
            });
        } else {
            var data = angular.copy($scope.DetailList);
            var arr = [];
            angular.forEach(data, function (item) {
                arr.push({
                    '部门': $scope.windowText,
                    '类别': $scope.titleName,
                    '乡镇': item.town,
                    '完成数': item.wcs,
                    '总数': item.zs
                })
            });
            if(arr.length < 1){
                $('.ensure').addClass("disable");
                $.message({
                    message: '暂无数据，导出失败',
                    type: 'error'
                });
            }else{
                var ws = XLSX.utils.json_to_sheet(arr,{header:['部门','类别','乡镇','完成数','总数']});
                var wb = XLSX.utils.book_new();
                XLSX.utils.book_append_sheet(wb, ws);
                XLSX.writeFile(wb, "截止"+ $scope.currentDate + $scope.windowText + $scope.titleName + "明细表.xlsx");
            }
        }
    }
});

// ***************************工作组织*****************************
app.controller('workListCtrl', function ($scope, $http, $timeout, $rootScope, $filter) {
    $scope.workListNavActive = 'ssfa';
    $scope.workListTitleName = '实施方案';
    $scope.state = 'stage';
    getIncressData($scope.workListNavActive,$scope.state);
    if ($rootScope.workListNavType != null) {
        $scope.workListNavActive = $rootScope.workListNavType;
        if ($rootScope.workListNavType == 'ssfa') {
            $scope.workListTitleName = '实施方案';
            $scope.state = 'stage';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if ($rootScope.workListNavType == 'dcjggs') {
            $scope.workListTitleName = '调查结果公示';
            getGSData($scope.workListNavActive);
        }
        if ($rootScope.workListNavType == 'sjjk') {
            $scope.workListTitleName = '数据建库';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if ($rootScope.workListNavType == 'nyjc') {
            $scope.workListTitleName = '内业检查';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if ($rootScope.workListNavType == 'wyjc') {
            $scope.workListTitleName = '外业检查';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if ($rootScope.workListNavType == 'sxlr') {
            $scope.workListTitleName = '属性录入';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if ($rootScope.workListNavType == 'yhyddy') {
            $scope.workListTitleName = '一户一档打印';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
    }
    $scope.workListNavClick = function (name) {
        $scope.workListNavActive = name;
        if (name == 'ssfa') {
            $scope.workListTitleName = '实施方案';
            $scope.state = 'stage';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if (name == 'dcjggs') {
            $scope.workListTitleName = '调查结果公示';
            getGSData($scope.workListNavActive);
        }
        if (name == 'sjjk') {
            $scope.workListTitleName = '数据建库';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if (name == 'nyjc') {
            $scope.workListTitleName = '内业检查';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if (name == 'wyjc') {
            $scope.workListTitleName = '外业检查';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if (name == 'sxlr') {
            $scope.workListTitleName = '属性录入';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
        if (name == 'yhyddy') {
            $scope.workListTitleName = '一户一档打印';
            $scope.state = 'percent';
            getIncressData($scope.workListNavActive,$scope.state);
        }
    };

    // 实施方案、数据建库、检查验收水流球图表配置
    function getIncressData(active,stage) {
        $http.post("http://120.24.219.92:8899/gzzz")
            .then(function (result) {
                var data = result.data.data;
                console.log(data);
                $scope.investigationTotal = 0;
                $scope.investigationCityTotal = data.length;
                if(stage == 'stage'){
                    for (var j = 0; j < data.length; j++) {
                        if (data[j][active]) {
                            $scope.investigationTotal++;
                            data[j].state = "已完成"
                        } else {
                            data[j].state = "未完成"
                        }
                    }
                }else if(stage == 'percent'){
                    for (var j = 0; j < data.length; j++) {
                        if (data[j][active] == 100) {
                            $scope.investigationTotal++;
                            data[j].state = data[j][active]
                        } else {
                            data[j].state = data[j][active]
                        }
                    }
                }
                $scope.investigationRate = $scope.investigationTotal / data.length;
                $scope.investigationList = data;
                var trainValue = $scope.investigationRate;
                var trainData = [trainValue, trainValue, trainValue];
                if (trainValue < 0.7) {
                    var trainColor = ['#ff2b42']
                } else if (trainValue < 0.9) {
                    var trainColor = ['#ff8f00']
                } else if (trainValue) {
                    var trainColor = ['#00cb00']
                }
                var echartsTrainOption = {

                    series: [{
                        color: trainColor,
                        type: 'liquidFill',
                        radius: '100%',
                        data: trainData,
                        backgroundStyle: {
                            borderWidth: 0,
                            borderColor: 'rgb(255,0,255,0.9)',
                            color: '#334E61'
                        },
                        outline: {
                            show: true,
                            borderDistance: 5,
                            itemStyle: {
                                borderColor: 'rgba(67,209,100,1)',
                                borderWidth: 0
                            }
                        },
                        label: {
                            normal: {
                                formatter: (trainValue * 100).toFixed(0) + '%',
                                textStyle: {
                                    color: '#fff',
                                    fontSize: 18
                                }
                            }
                        }
                    }]
                };
                var echartsTrainChart = echarts.init(document.getElementById("echartsTrain"));
                echartsTrainChart.setOption(echartsTrainOption);
            })
    }

    $scope.rev = false;

    // 调查结果公示水流球图表配置
    function getGSData(active) {
        $http.post("http://120.24.219.92:8899/dcjggs")
            .then(function (result) {
                var data = result.data.data;
                console.log(data);
                $scope.investigationTotal = 0;
                $scope.investigationCityTotal = 0;
                for (var j = 0; j < data.length; j++) {
                    $scope.investigationCityTotal += data[j].zs;
                    $scope.investigationTotal += data[j].wcs;
                    data[j].value = data[j].wcs / data[j].zs;
                }
                $scope.investigationRate = $scope.investigationTotal / $scope.investigationCityTotal;
                function sortrate(a, b) {
                    return b.value - a.value
                }
                function sortcode(a, b) {
                    return a.code - b.code
                }
                data.sort(sortrate);
                $scope.investigationList = data;
                var trainValue = $scope.investigationRate;
                var trainData = [trainValue, trainValue, trainValue];
                if (trainValue < 0.7) {
                    var trainColor = ['#ff2b42']
                } else if (trainValue < 0.9) {
                    var trainColor = ['#ff8f00']
                } else if (trainValue) {
                    var trainColor = ['#00cb00']
                }
                var echartsTrainOption = {

                    series: [{
                        color: trainColor,
                        type: 'liquidFill',
                        radius: '100%',
                        data: trainData,
                        backgroundStyle: {
                            borderWidth: 0,
                            borderColor: 'rgb(255,0,255,0.9)',
                            color: '#334E61'
                        },
                        outline: {
                            show: true,
                            borderDistance: 5,
                            itemStyle: {
                                borderColor: 'rgba(67,209,100,1)',
                                borderWidth: 0
                            }
                        },
                        label: {
                            normal: {
                                formatter: (trainValue * 100).toFixed(0) + '%',
                                textStyle: {
                                    color: '#fff',
                                    fontSize: 18
                                }
                            }
                        }
                    }]
                };
                var echartsTrainChart = echarts.init(document.getElementById("echartsTrain"));
                echartsTrainChart.setOption(echartsTrainOption);
                $scope.RankClick = function () {
                    if ($scope.rev) {
                        //利用js中的sort方法
                        $scope.investigationList.sort(sortrate);
                    } else {
                        $scope.investigationList.sort(sortcode);
                    }
                    $scope.rev = !$scope.rev;
                };
            })
    }

    $scope.currentDate = $filter("date")(new Date(), "yyyy-MM-dd");

    // 调查结果公示数据列表
    $scope.listDetailShow = false;
    $scope.windowText = '';
    $scope.listClick = function (item) {
        $scope.windowText = item.name;
        $scope.listDetailShow = true;
        $http.post('http://120.24.219.92:8899/dcjggsbycode', {
            code: item.code
        }).then(function (result) {
            console.log(result.data.data);
            var data = result.data.data
            $scope.dcjggsDetailList = data;
            for(var a=0;a<data.length;a++ ){
                data[a].per = data[a].wcs/data[a].zs;
            }
        })
    };
    $scope.closeWindow = function () {
        $scope.listDetailShow = false;
    };
    $scope.windowTime = '';

});

// ***************************权籍调查*****************************
app.controller('investigationCtrl', function ($scope, $http, $rootScope, $filter) {
    // 权籍调查情况
    // 农村宅基地
    $scope.titleName = '农村宅基地';
    $scope.windowItem = {};
    $scope.flag = 0;
    $scope.selectAddress = '湖南省';
    $scope.investigationNavActive = '0';
    if ($rootScope.navType != null) {
        if ($rootScope.navType == 'nc') {
            $scope.titleName = '农村宅基地';
            $scope.investigationNavActive = '0';
        }
        if ($rootScope.navType == 'jt') {
            $scope.titleName = '集体建设用地';
            $scope.investigationNavActive = '1';
        }
    }
    getIncressData(0, $scope.investigationNavActive);

    $scope.investigationNavClick = function (name) {
        $scope.investigationNavActive = name;
        if (name == '1') {
            $scope.titleName = '集体建设用地';
        }
        if (name == '0') {
            $scope.titleName = '农村宅基地';
        }
        $scope.selectArea('city');
    };

    $scope.currentDate = $filter("date")(new Date(), "yyyy-MM-dd");
    $scope.rev = false;

    function getIncressData(code, state) {
        $http.post("http://120.24.219.92:8899/qjdc", {code: code}
        )
            .then(function (result) {
                var data = result.data.data;
                $scope.investigationCurrentCount = 0;
                $scope.investigationTotal = 0;
                if (state === '0') {
                    for (var i = 0; i < data.length; i++) {
                        $scope.investigationCurrentCount += data[i].zjd;
                        $scope.investigationTotal += data[i].zjdCount;
                        data[i].rate = data[i].zjd / data[i].zjdCount;
                        data[i].wcs = data[i].zjd;
                        if(code == "1" || code == "2"){
                            if(data[i].dept.length == "5"){
                                data[i].dept = (data[i].dept).substring(0,4);
                            }else{
                                data[i].dept = (data[i].dept).substring(2,6);
                            }
                        }
                    }
                } else if (state === '1') {
                    for (var j = 0; j < data.length; j++) {
                        $scope.investigationCurrentCount += data[j].jsyd;
                        $scope.investigationTotal += data[j].jsydCount;
                        data[j].rate = data[j].jsyd / data[j].jsydCount;
                        data[j].wcs = data[j].jsyd;
                        if(code == "1" || code == "2"){
                            if(data[j].dept.length == "5"){
                                data[j].dept = (data[j].dept).substring(0,4);
                            }else{
                                data[j].dept = (data[j].dept).substring(2,6);
                            }
                        }
                    }
                }
                function sortrate(a, b) {
                    return b.rate - a.rate
                }
                function sortcode(a, b) {
                    return a.code - b.code
                }
                data.sort(sortrate);
                $scope.investigationRate = $scope.investigationCurrentCount / $scope.investigationTotal;
                $scope.investigationList = data;
                var trainValue = $scope.investigationRate;
                var trainData = [trainValue, trainValue, trainValue];
                if (trainValue < 0.60) {
                    var trainColor = ['#ff2b42']
                } else if (trainValue < 0.9) {
                    var trainColor = ['#ff8f00']
                } else if (trainValue) {
                    var trainColor = ['#00cb00']
                }
                var echartsTrainOption = {

                    series: [{
                        color: trainColor,
                        type: 'liquidFill',
                        radius: '100%',
                        data: trainData,
                        backgroundStyle: {
                            borderWidth: 0,
                            borderColor: 'rgb(255,0,255,0.9)',
                            color: '#334E61'
                        },
                        outline: {
                            show: true,
                            borderDistance: 5,
                            itemStyle: {
                                borderColor: 'rgba(67,209,100,1)',
                                borderWidth: 0
                            }
                        },
                        label: {
                            normal: {
                                formatter: (trainValue * 100).toFixed(2) + '%',
                                textStyle: {
                                    color: '#fff',
                                    fontSize: 18
                                }
                            }
                        }
                    }]
                };
                var echartsTrainChart = echarts.init(document.getElementById("echartsTrain"));
                echartsTrainChart.setOption(echartsTrainOption);

                // 农村排序升降序
                $scope.RankClick = function () {
                    if ($scope.rev) {
                        //利用js中的sort方法
                        $scope.investigationList.sort(sortrate);
                    } else {
                        $scope.investigationList.sort(sortcode);
                    }
                    $scope.rev = !$scope.rev;
                };
            })
    }

    $scope.listDetailShow = false;
    $scope.windowText = '';
    $scope.listClick = function (item) {
        $scope.windowItem = item;
        $scope.windowText = item.name;
        $scope.listDetailShow = true;
        var code;
        if ($scope.flag == 0) {
            code = item.code.substr(0, 4);
        } else if ($scope.flag == 1) {
            code = item.code.substr(0, 6);
        } else {
            code = item.code.substr(0, 9);
        }
        $http.post('http://120.24.219.92:8899/qjdcbydate', {
            code: code,
            stage: 0,
            type: $scope.investigationNavActive
        }).then(function (result) {
            var data = result.data.data;
            console.log(data);
            $scope.windowData = data;
            var dateArr = [];
            var countArr = [];
            for (var i = 0; i < data.length; i++) {
                dateArr.push(data[i].date);
                countArr.push(data[i].count);
            }
            // countArr.reverse();
            // dateArr.reverse();
            var maxNumbers = Math.max.apply(Math, countArr) + 200;
            var minNumbers = Math.min.apply(Math, countArr) - 200;
            if (minNumbers < 0) {
                minNumbers = 0
            }
            var windowEchartsOption = {
                grid: {
                    top: 30,
                    left: 60,
                    right: 10,
                    bottom: 40
                },
                color: ['#000000'],
                xAxis: {
                    type: 'category',
                    data: dateArr,
                    nameTextStyle: {
                        fontSize: 12
                    },
                    axisLabel: {
                        color: '#fff',
                        interval: 0,
                        rotate: 30
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#ccc'
                        }
                    }
                },
                yAxis: {
                    min: minNumbers,
                    max: maxNumbers,
                    name: '宗',
                    nameTextStyle: {
                        color: '#fff'
                    },
                    type: 'value',
                    axisLabel: {
                        color: '#fff'
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#ccc'
                        }
                    }
                },
                series: [{
                    data: countArr,
                    type: 'line',
                    label: {
                        show: true,
                        position: 'top'
                    }
                }]
            };
            var windowEchartsChart = echarts.init(document.getElementById("window-body-charts"));
            windowEchartsChart.setOption(windowEchartsOption);
        })
    };
    $scope.closeWindow = function () {
        $scope.listDetailShow = false;
        $scope.listDetailShow2 = false;
    };

    $scope.excel = function () {
        if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
            $('.ensure').addClass("disable");
            $.message({
                message: '移动端不支持导出功能，请在PC端操作！',
                type: 'error'
            });
        } else {
            var data = angular.copy($scope.windowData);
            var arr = [];
            angular.forEach(data, function (item) {
                arr.push({
                    '行政区': $scope.windowText,
                    '类别': $scope.titleName + '权籍调查',
                    '当日完成数': item.count,
                    '日期': item.date
                })
            });
            if(arr.length < 1){
                $('.ensure').addClass("disable");
                $.message({
                    message: '暂无数据，导出失败',
                    type: 'error'
                });
            }else{
                var ws = XLSX.utils.json_to_sheet(arr,{header:['行政区','类别','当日完成数','日期']});
                var wb = XLSX.utils.book_new();
                XLSX.utils.book_append_sheet(wb, ws);
                XLSX.writeFile(wb, $scope.windowText + $scope.titleName + "权籍调查日增量.xlsx");
            }
        }
    };

    // 农村宅基地地区筛选
    $scope.area = 'city';
    $scope.code = '';
    $scope.ncShowCounty = false;
    $scope.ncShowTown = false;
    $scope.ncindex = true;
    $scope.ncdept = false;
    $scope.ncspeed = true;
    $scope.selectArea = function (name) {
        $scope.area = name;
        if ($scope.area == 'county') {
            $scope.rev = false;
            // select(0, $scope.area);
            getIncressData(1,$scope.investigationNavActive);
            $scope.ncShowCounty = true;
            $scope.ncShowTown = false;
            $scope.flag = 1;
            $scope.ncindex = false;
            $scope.ncdept = true;
            $scope.ncspeed = false;
        } else if ($scope.area == 'town') {
            // if ($scope.code == '') {
            //     $('.ensure').addClass("disable");
            //     $.message({
            //         message: '请先选择上一级行政单位',
            //         type: 'error'
            //     });
            // } else {
            //     $('.ensure').removeClass("disable")
            // }
            $scope.rev = false;
            getIncressData(2,$scope.investigationNavActive);
            // select($scope.code, $scope.area);
            $scope.ncShowCounty = false;
            $scope.ncShowTown = true;
            $scope.flag = 2;
            $scope.ncindex = false;
            $scope.ncdept = true;
            $scope.ncspeed = false;
        } else {
            $scope.rev = false;
            getIncressData(0, $scope.investigationNavActive);
            $scope.ncShowCounty = false;
            $scope.ncShowTown = false;
            $scope.flag = 0;
            $scope.ncindex = true;
            $scope.ncdept = false;
            $scope.ncspeed = true;
        }

    };

    // 地区选择
    function select(code, state) {
        $http.post('http://120.24.219.92:8899/qjdc', {code: code}).then(function (result) {
            $('.mobileSelect').remove();
            var demoData3 = result.data.data;
            for (var i = 0; i < demoData3.length; i++) {
                demoData3[i].value = demoData3[i].name
            }
            console.log(demoData3);
            if (state == 'county') {
                var ncCountySelect2 = new MobileSelect({
                    trigger: '#nc-county-select',
                    title: '选择市州',
                    titleColor: '#0c2344',
                    ensureBtnColor: '#e5fffd',
                    cancelBtnColor: '#e5fffd',
                    textColor: 'rgba(12,35,68,0.8)',
                    wheels: [
                        {data: demoData3}
                    ],
                    triggerDisplayData: false,
                    onShow: function (e) {
                    },
                    transitionEnd: function (indexArr, data) {
                    },
                    callback: function (indexArr, data) {
                        $('#nc-county-select').html(`
                <span>` + data[0].name + `</span>
                <i class="icon-down background-size"></i>`);
                        $scope.code = data[0].code.substr(0, 4);
                        $scope.selectAddress = data[0].name;
                        getIncressData($scope.code, $scope.investigationNavActive)
                    }
                });
            } else if (state == 'town') {
                var ncCountySelect3 = new MobileSelect({
                    trigger: '#nc-town-select',
                    title: '选择区县',
                    titleColor: '#0c2344',
                    ensureBtnColor: '#e5fffd',
                    cancelBtnColor: '#e5fffd',
                    textColor: 'rgba(12,35,68,0.8)',
                    wheels: [
                        {data: demoData3}
                    ],
                    triggerDisplayData: false,
                    onShow: function (e) {
                    },
                    transitionEnd: function (indexArr, data) {
                    },
                    callback: function (indexArr, data) {
                        $('#nc-town-select').html(`
                <span>` + data[0].name + `</span>
                <i class="icon-down background-size"></i>`);
                        var code = data[0].code.substr(0, 6);
                        $scope.selectAddress = data[0].name;
                        getIncressData(code, $scope.investigationNavActive)
                    }
                });
            }

        });
    }

    //乡镇搜索
    $scope.searchname = '';
    $scope.listDetailShow2 = false;
    $scope.Search = function () {
        $scope.listDetailShow2 = true;
        $http.post('http://120.24.219.92:8899/qjdcbyname',
            {name: $scope.searchname}
        ).then(function (result) {
            var data  = result.data.data;
            if($scope.titleName == '农村宅基地'){
                for(var i=0;i<data.length;i++){
                    data[i].wcs = data[i].zjd;
                    data[i].zs = data[i].zjdCount;
                }
            }else if($scope.titleName == '集体建设用地'){
                for(var i=0;i<data.length;i++){
                    data[i].wcs = data[i].jsyd;
                    data[i].zs = data[i].jsydCount;
                }
            }
            console.log(data);
            $scope.DetailList = data;
        })
    }
});

// ***************************编辑成图*****************************
app.controller('registerBookCtrl', function ($scope, $http, $rootScope, $filter) {
    // 编辑成图
    // 农村宅基地
    $scope.titleName = '农村宅基地';
    $scope.windowItem = {};
    $scope.flag = 0;
    $scope.selectAddress = '湖南省';
    $scope.investigationNavActive = '0';
    if ($rootScope.navType != null) {
        if ($rootScope.navType == 'nc') {
            $scope.titleName = '农村宅基地';
            $scope.investigationNavActive = '0';
        }
        if ($rootScope.navType == 'jt') {
            $scope.titleName = '集体建设用地';
            $scope.investigationNavActive = '1';
        }
    }
    getIncressData(0, $scope.investigationNavActive);

    $scope.investigationNavClick = function (name) {
        $scope.investigationNavActive = name;
        if (name == '1') {
            $scope.titleName = '集体建设用地';
        }
        if (name == '0') {
            $scope.titleName = '农村宅基地';
        }
        $scope.selectArea('city');
    };

    $scope.currentDate = $filter("date")(new Date(), "yyyy-MM-dd");
    $scope.rev = false;

    function getIncressData(code, state) {
        $http.post("http://120.24.219.92:8899/bdccl", {code: code}
        )
            .then(function (result) {
                var data = result.data.data;
                console.log(data);
                $scope.investigationCurrentCount = 0;
                $scope.investigationTotal = 0;
                if (state == '0') {
                    for (var i = 0; i < data.length; i++) {
                        $scope.investigationCurrentCount += data[i].zjd;
                        $scope.investigationTotal += data[i].zjdCount;
                        data[i].rate = data[i].zjd / data[i].zjdCount;
                        data[i].wcs = data[i].zjd;
                        if(code == "1" || code == "2"){
                            if(data[i].dept.length == "5"){
                                data[i].dept = (data[i].dept).substring(0,4);
                            }else{
                                data[i].dept = (data[i].dept).substring(2,6);
                            }
                        }
                    }
                } else if (state == '1') {
                    for (var j = 0; j < data.length; j++) {
                        $scope.investigationCurrentCount += data[j].jsyd;
                        $scope.investigationTotal += data[j].jsydCount;
                        data[j].rate = data[j].jsyd / data[j].jsydCount;
                        data[j].wcs = data[j].jsyd;
                        if(code == "1" || code == "2"){
                            if(data[j].dept.length == "5"){
                                data[j].dept = (data[j].dept).substring(0,4);
                            }else{
                                data[j].dept = (data[j].dept).substring(2,6);
                            }
                        }
                    }
                }
                $scope.investigationRate = $scope.investigationCurrentCount / $scope.investigationTotal;
                $scope.investigationList = data;
                function sortrate(a, b) {
                    return b.rate - a.rate
                }
                function sortcode(a, b) {
                    return a.code - b.code
                }
                data.sort(sortrate);
                var trainValue = $scope.investigationRate;
                var trainData = [trainValue, trainValue, trainValue];
                if (trainValue < 0.60) {
                    var trainColor = ['#ff2b42']
                } else if (trainValue < 0.9) {
                    var trainColor = ['#ff8f00']
                } else if (trainValue) {
                    var trainColor = ['#00cb00']
                }
                var echartsTrainOption = {

                    series: [{
                        color: trainColor,
                        type: 'liquidFill',
                        radius: '100%',
                        data: trainData,
                        backgroundStyle: {
                            borderWidth: 0,
                            borderColor: 'rgb(255,0,255,0.9)',
                            color: '#334E61'
                        },
                        outline: {
                            show: true,
                            borderDistance: 5,
                            itemStyle: {
                                borderColor: 'rgba(67,209,100,1)',
                                borderWidth: 0
                            }
                        },
                        label: {
                            normal: {
                                formatter: (trainValue * 100).toFixed(2) + '%',
                                textStyle: {
                                    color: '#fff',
                                    fontSize: 18
                                }
                            }
                        }
                    }]
                };
                var echartsTrainChart = echarts.init(document.getElementById("echartsTrain"));
                echartsTrainChart.setOption(echartsTrainOption);

                // 农村排序升降序
                $scope.RankClick = function () {
                    if ($scope.rev) {
                        //利用js中的sort方法
                        $scope.investigationList.sort(sortrate);
                    } else {
                        $scope.investigationList.sort(sortcode);
                    }
                    $scope.rev = !$scope.rev;
                };
            })
    }

    $scope.listDetailShow = false;
    $scope.windowText = '';
    $scope.listClick = function (item) {
        $scope.windowItem = item;
        $scope.windowText = item.name;
        $scope.listDetailShow = true;
        var code;
        if ($scope.flag == 0) {
            code = item.code.substr(0, 4);
        } else if ($scope.flag == 1) {
            code = item.code.substr(0, 6);
        } else {
            code = item.code.substr(0, 9);
        }
        $http.post('http://120.24.219.92:8899/qjdcbydate', {
            code: code,
            stage: 1,
            type: $scope.investigationNavActive
        }).then(function (result) {
            var data = result.data.data;
            console.log(data);
            $scope.windowData = data;
            var dateArr = [];
            var countArr = [];
            for (var i = 0; i < data.length; i++) {
                dateArr.push(data[i].date);
                countArr.push(data[i].count);
            }
            // countArr.reverse();
            // dateArr.reverse();
            var maxNumbers = Math.max.apply(Math, countArr) + 200;
            var minNumbers = Math.min.apply(Math, countArr) - 200;
            if (minNumbers < 0) {
                minNumbers = 0
            }
            var windowEchartsOption = {
                grid: {
                    top: 30,
                    left: 60,
                    right: 10,
                    bottom: 40
                },
                color: ['#000000'],
                xAxis: {
                    type: 'category',
                    data: dateArr,
                    nameTextStyle: {
                        fontSize: 12
                    },
                    axisLabel: {
                        color: '#fff',
                        interval: 0,
                        rotate: 30
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#ccc'
                        }
                    }
                },
                yAxis: {
                    min: minNumbers,
                    max: maxNumbers,
                    name: '宗',
                    nameTextStyle: {
                        color: '#fff'
                    },
                    type: 'value',
                    axisLabel: {
                        color: '#fff'
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#ccc'
                        }
                    }
                },
                series: [{
                    data: countArr,
                    type: 'line',
                    label: {
                        show: true,
                        position: 'top'
                    }
                }]
            };
            var windowEchartsChart = echarts.init(document.getElementById("window-body-charts"));
            windowEchartsChart.setOption(windowEchartsOption);
        })
    };
    $scope.closeWindow = function () {
        $scope.listDetailShow = false;
        $scope.listDetailShow2 = false;
    };


    // 农村宅基地地区筛选
    $scope.area = 'city';
    $scope.code = '';
    $scope.ncShowCounty = false;
    $scope.ncShowTown = false;
    $scope.ncindex = true;
    $scope.ncdept = false;
    $scope.ncspeed = true;
    $scope.selectArea = function (name) {
        $scope.area = name;
        if ($scope.area == 'county') {
            $scope.rev = false;
            getIncressData(1, $scope.investigationNavActive);
            $scope.ncShowCounty = true;
            $scope.ncShowTown = false;
            $scope.flag = 1;
            $scope.ncindex = false;
            $scope.ncdept = true;
            $scope.ncspeed = false;
        } else if ($scope.area == 'town') {
            // if ($scope.code == '') {
            //     $('.ensure').addClass("disable");
            //     $.message({
            //         message: '请先选择上一级行政单位',
            //         type: 'error'
            //     });
            // } else {
            //     $('.ensure').removeClass("disable")
            // }
            $scope.rev = false;
            getIncressData(2, $scope.investigationNavActive);
            $scope.ncShowCounty = false;
            $scope.ncShowTown = true;
            $scope.flag = 2;
            $scope.ncindex = false;
            $scope.ncdept = true;
            $scope.ncspeed = false;
        } else {
            $scope.rev = false;
            getIncressData(0, $scope.investigationNavActive);
            $scope.ncShowCounty = false;
            $scope.ncShowTown = false;
            $scope.flag = 0;
            $scope.ncindex = true;
            $scope.ncdept = false;
            $scope.ncspeed = true;
        }

    };

    // 地区选择
    function select(code, state) {
        $http.post('http://120.24.219.92:8899/qjdc', {code: code}).then(function (result) {
            $('.mobileSelect').remove();
            var demoData3 = result.data.data;
            for (var i = 0; i < demoData3.length; i++) {
                demoData3[i].value = demoData3[i].name
            }
            console.log(demoData3);
            if (state == 'county') {
                var ncCountySelect2 = new MobileSelect({
                    trigger: '#nc-county-select',
                    title: '选择市州',
                    titleColor: '#0c2344',
                    ensureBtnColor: '#e5fffd',
                    cancelBtnColor: '#e5fffd',
                    textColor: 'rgba(12,35,68,0.8)',
                    wheels: [
                        {data: demoData3}
                    ],
                    triggerDisplayData: false,
                    onShow: function (e) {
                    },
                    transitionEnd: function (indexArr, data) {
                    },
                    callback: function (indexArr, data) {
                        $('#nc-county-select').html(`
                <span>` + data[0].name + `</span>
                <i class="icon-down background-size"></i>`);
                        $scope.code = data[0].code.substr(0, 4);
                        $scope.selectAddress = data[0].name;
                        getIncressData($scope.code, $scope.investigationNavActive)
                    }
                });
            } else if (state == 'town') {
                var ncCountySelect3 = new MobileSelect({
                    trigger: '#nc-town-select',
                    title: '选择区县',
                    titleColor: '#0c2344',
                    ensureBtnColor: '#e5fffd',
                    cancelBtnColor: '#e5fffd',
                    textColor: 'rgba(12,35,68,0.8)',
                    wheels: [
                        {data: demoData3}
                    ],
                    triggerDisplayData: false,
                    onShow: function (e) {
                    },
                    transitionEnd: function (indexArr, data) {
                    },
                    callback: function (indexArr, data) {
                        $('#nc-town-select').html(`
                <span>` + data[0].name + `</span>
                <i class="icon-down background-size"></i>`);
                        var code = data[0].code.substr(0, 6);
                        $scope.selectAddress = data[0].name;
                        getIncressData(code, $scope.investigationNavActive)
                    }
                });
            }

        });
    }

    $scope.excel = function () {
        if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
            $('.ensure').addClass("disable");
            $.message({
                message: '移动端不支持导出功能，请在PC端操作！',
                type: 'error'
            });
        } else {
            var data = angular.copy($scope.windowData);
            var arr = [];
            angular.forEach(data, function (item) {
                arr.push({
                    '行政区': $scope.windowText,
                    '类别': $scope.titleName + '编辑成图',
                    '当日完成数': item.count,
                    '日期': item.date
                })
            });
            if(arr.length < 1){
                $('.ensure').addClass("disable");
                $.message({
                    message: '暂无数据，导出失败',
                    type: 'error'
                });
            }else{
                var ws = XLSX.utils.json_to_sheet(arr,{header:['行政区','类别','当日完成数','日期']});
                var wb = XLSX.utils.book_new();
                XLSX.utils.book_append_sheet(wb, ws);
                XLSX.writeFile(wb, $scope.windowText + $scope.titleName + "编辑成图日增量.xlsx");
            }
        }
    };

    //乡镇搜索
    $scope.searchname = '';
    $scope.listDetailShow2 = false;
    $scope.Search = function () {
        $scope.listDetailShow2 = true;
        $http.post('http://120.24.219.92:8899/bdcclbyname',
            {name: $scope.searchname}
        ).then(function (result) {
            var data  = result.data.data;
            if($scope.titleName == '农村宅基地'){
                for(var i=0;i<data.length;i++){
                    data[i].wcs = data[i].zjd;
                    data[i].zs = data[i].zjdCount;
                }
            }else if($scope.titleName == '集体建设用地'){
                for(var i=0;i<data.length;i++){
                    data[i].wcs = data[i].jsyd;
                    data[i].zs = data[i].jsydCount;
                }
            }
            console.log(data);
            $scope.DetailList = data;
        })
    }
});

// ***************************每日汇总*****************************
app.controller('aggregeCtrl', function ($scope, $http,$filter,$cookies) {
    var userid = $cookies.get('userid');
    // var userid = 'a02099afec4c4dffb467a6a6143aaa70';
    $scope.titleName = '权籍调查农村宅基地';
    $scope.investigationNavActive = '0';
    $scope.stage = '0';
    $scope.type = '0';

    $scope.investigationNavClick = function (name) {
        $scope.investigationNavActive = name;
        if (name == '0') {
            $scope.titleName = '权籍调查农村宅基地';
            $scope.stage = '0';
            $scope.type = '0';
        }
        if (name == '1') {
            $scope.titleName = '权籍调查集体建设用地';
            $scope.stage = '0';
            $scope.type = '1';
        }
        if (name == '2') {
            $scope.titleName = '编辑成图农村宅基地';
            $scope.stage = '1';
            $scope.type = '0';
        }
        if (name == '3') {
            $scope.titleName = '编辑成图集体建设用地';
            $scope.stage = '1';
            $scope.type = '1';
        }
    };

    $scope.currentDate = $filter("date")(new Date(), "yyyy-MM-dd");
    Calendar.create({
        classN: 'calendar-item', // 这里的calendar-item可随意填 不需要跟我一样
        callBack: function(bindElem, dateObj) {
            // bindElem: 该控件绑定的元素
            // dateObj: 选中的年、月、日 如： {year: 2018, month: 8, date: 12}
            // 用户可通过bindElem和dateObj搞事情啦 😆
            var date = dateObj.year + '-' + dateObj.month + '-' + dateObj.date;
            bindElem.innerHTML = date + "<i class=\"icon-down background-size\" ></i>";
            $scope.currentDate = date;
        }
    });

    $http.post("http://120.24.219.92:8899/findmytowns", {
        userid: userid
    })
        .then(function (result) {
            var data = result.data.data;
            console.log(data);
            $scope.townList = data;
        });

    $scope.submit = function(){
        for (var i = 0; i < $scope.townList.length; i++) {
            if($scope.townList[i].count){
                $http.post("http://120.24.219.92:8899/addhouse", {
                    stage: $scope.stage,
                    type: $scope.type,
                    townCode: $scope.townList[i].code,
                    count: $scope.townList[i].count,
                    date: $scope.currentDate
                })
                    .then(function (result) {
                        console.log(result);
                        if(result.data.msg =="成功"){
                            $('.ensure').addClass("success");
                            $.message({
                                message: '上传成功',
                                type: 'success'
                            });
                        }else{
                            $('.ensure').addClass("disable");
                            $.message({
                                message: '上传失败',
                                type: 'error'
                            });
                        }
                    });
                $scope.townList[i].count = null;
            }

        }
    }

});

// ***************************完成情况*****************************
app.controller('stageCtrl', function ($scope, $http, $cookies) {
    var id = $cookies.get('userid');
    // var id = 'a02099afec4c4dffb467a6a6143aaa70';
    $scope.titleName = '区县级';
    $scope.investigationNavActive = '0';
    $scope.quShow = true;
    $scope.xiangShow = false;
    QuShow(id);

    $scope.investigationNavClick = function (name) {
        $scope.investigationNavActive = name;
        if (name == '0') {
            $scope.titleName = '区县级';
            $scope.quShow = true;
            $scope.xiangShow = false;
            QuShow(id);
        }
        if (name == '1') {
            $scope.titleName = '乡镇级';
            $scope.quShow = false;
            $scope.xiangShow = true;
            XiangShow(id);
        }
    };

    function QuShow(id){
        $http.post("http://120.24.219.92:8899/findmycountries", {
            userid: id
        })
            .then(function (result) {
                var data  = result.data.data;
                for(var i=0; i<data.length; i++){
                    data[i].change = false;
                }
                $scope.quList = data;
                console.log($scope.quList);
            });
    }

    function XiangShow(id){
        $http.post("http://120.24.219.92:8899/findmytowns", {
            userid: id
        })
            .then(function (result) {
                var data  = result.data.data;
                for(var i=0; i<data.length; i++){
                    data[i].change = false;
                }
                $scope.townList = data;
                console.log($scope.townList);
            });
    }

    $scope.quChange = function(index){
        $scope.quList[index].change = true;
    };
    $scope.xiangChange = function(index){
        $scope.townList[index].change  = true;
    };

    $scope.submitQu = function(){
        for (var i = 0; i < $scope.quList.length; i++) {
            if($scope.quList[i].change){
                $http.post("http://120.24.219.92:8899/editcountry", {
                    code: $scope.quList[i].code,
                    ssfa: $scope.quList[i].ssfa,
                    sjjk: $scope.quList[i].sjjk,
                    nyjc: $scope.quList[i].nyjc,
                    wyjc: $scope.quList[i].wyjc,
                    sxlr: $scope.quList[i].sxlr,
                    yhyddy: $scope.quList[i].yhyddy
                })
                    .then(function (result) {
                        console.log(result);
                        if(result.data.msg =="成功"){
                            $('.ensure').addClass("success");
                            $.message({
                                message: '上传成功',
                                type: 'success'
                            });
                        }else{
                            $('.ensure').addClass("disable");
                            $.message({
                                message: '上传失败',
                                type: 'error'
                            });
                        }
                    });
            }
        }
    };

    $scope.submitXiang = function(){
        for (var i = 0; i < $scope.townList.length; i++) {
            if($scope.townList[i].change){
                $http.post("http://120.24.219.92:8899/edittown", {
                    code: $scope.townList[i].code,
                    dcjggs: $scope.townList[i].dcjggs
                })
                    .then(function (result) {
                        console.log(result);
                        if(result.data.msg =="成功"){
                            $('.ensure').addClass("success");
                            $.message({
                                message: '上传成功',
                                type: 'success'
                            });
                        }else{
                            $('.ensure').addClass("disable");
                            $.message({
                                message: '上传失败',
                                type: 'error'
                            });
                        }
                    });
            }
        }
    }

});
